package cn.odore.basic.service.impl;

import cn.odore.basic.mapper.BasicMapper;
import cn.odore.basic.query.BasicQuery;
import cn.odore.basic.service.IBasicService;
import cn.odore.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BasicServiceImpl<T> implements IBasicService<T> {
    @Autowired
    private BasicMapper<T> basicMapper;

    @Override
    public List<T> getAll() {
        return basicMapper.loadAll();
    }

    @Override
    public PageList<T> list(BasicQuery query) {
        Long total = basicMapper.queryCount(query);
        if (total == null || total.intValue() < 1){
            return new PageList<>();
        }
        List<T> list = basicMapper.queryData(query);

        return new PageList<>(total, list);
    }

    @Override
    @Transactional
    public void add(T t) {
        basicMapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        basicMapper.update(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        basicMapper.delete(id);
    }

}
